package com.zany.children.action;

import com.zany.children.service.ClassService;
import com.zany.children.vo.Class;
import com.zany.system.vo.UserInfoVO;
import com.zany.util.ZanyErrorMsg;
import com.zany.util.ZanyException;
import com.zany.util.action.AbstractActionSupport;
import com.zany.util.ajax.AjaxTools;
import com.zany.util.json.JSONException;
import com.zany.util.json.JSONObject;
import com.zany.util.json.JsonTools;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import java.util.List;
import java.util.Map;

public class ClassAction extends AbstractActionSupport {
    private static Log logger = LogFactory.getLog(ClassAction.class);
    public Class getClazz() {
        return clazz;
    }

    public void setClazz(Class clazz) {
        this.clazz = clazz;
    }

    private Class clazz;

    private ClassService classService;

    public ClassService getClassService() {
        return classService;
    }

    public void setClassService(ClassService classService) {
        this.classService = classService;
    }


    private Map session;

    /**
     * 查看班级列表.
     *
     * @throws ZanyException
     * @throws JSONException
     */
    public void list() throws ZanyException, JSONException {
        String start = request.getParameter("start");
        String name = request.getParameter("name");

//        HttpSession session = ServletActionContext.getRequest().getSession();
        UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
        response = ServletActionContext.getResponse();
        String pageStart = StringUtils.isBlank(start) ? "1" : start;
        Class clazz = new Class();
        clazz.setName(name);
        if (userInfo != null) {
            clazz.setOrgCode(userInfo.getUserOrg());
        }
        clazz.setPageMessage(Integer.valueOf(pageStart));
        Integer pageTotal = classService.getClassListPageCount(clazz);
        List<Class> classListPage = classService.getClassListPage(clazz);
        AjaxTools.exAjax("{results:" + pageTotal + ",ZANY:" + JsonTools.lstBeanToJson(classListPage) + "}", response);
    }


    /**
     * 根据id查看详细 信息.
     *
     * @throws ZanyException
     */
    public void detail() throws ZanyException {
        String id = request.getParameter("id");
        response = ServletActionContext.getResponse();
        Class clazz = classService.getClassById(Long.parseLong(id));
        JSONObject jsonObject = JsonTools.beanToJson(clazz);
        String json = jsonObject.toString();
        AjaxTools.exAjax("{results:" + 1 + ",ZANY:" + json + "}", response);
    }


    /**
     * 保存班级信息.
     * @throws ZanyException
     */
    public void save() throws ZanyException {
        String name = request.getParameter("name");
        response = ServletActionContext.getResponse();
        Class clazz = new Class();
        try {
            clazz.setName(name);
            UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
            if (userInfo != null) {
                clazz.setOrgCode(userInfo.getUserOrg());
                clazz.setCreater(userInfo.getUserName());
            }
            Integer count = classService.getClassListPageCount(clazz);
            if (count != 0) {
                AjaxTools.exAjax("{\"success\":false,msg:\"班级已存在！\"}", response);
            } else {
                classService.saveClass(clazz);
                AjaxTools.exAjax("{success:true}", response);
            }
        } catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT, e.getMessage(), e);
        }
    }



    public void update()throws ZanyException {
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        response = ServletActionContext.getResponse();
        Class clazz = new Class();
        try {
            clazz.setName(name);
            UserInfoVO userInfo = (UserInfoVO) session.get("userInfo");
            if (userInfo != null) {
                clazz.setOrgCode(userInfo.getUserOrg());
            }
            Integer count = classService.getClassListPageCount(clazz);
            if (count != 0) {
                AjaxTools.exAjax("{\"success\":false,msg:\"班级已存在！\"}", response);
            } else {
                clazz.setId(Long.parseLong(id));
                classService.updateClass(clazz);
                AjaxTools.exAjax("{success:true}", response);
            }
        } catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT, e.getMessage(), e);
        }
    }



    public void delete() throws ZanyException{
        if(logger.isInfoEnabled()){logger.info("ClassAction.delete()");}
        response = ServletActionContext.getResponse();
        String delId[] = request.getParameterValues("delId");
        try{
            if(delId.length>0){
                for (int i = 0; i < delId.length; i++){
                    classService.deleteClassById(Long.parseLong(delId[i]));
                }
                AjaxTools.exAjax("{success:true}", response);
            }
        }catch (Exception e) {
            AjaxTools.exAjax("{success:false}", response);
            throw new ZanyException(ZanyErrorMsg.ERR_DEFAULT,e.getMessage(),e);
        }
    }
    @Override
    public void setSession(Map map) {
        session = map;
    }
}
